﻿Namespace UI.Windows
	Public Class Archive

		Public Sub New()

			' This call is required by the designer.
			InitializeComponent()

			' Add any initialization after the InitializeComponent() call.
			Me.DataContext = New ViewModels.ArchiveModel
		End Sub

		''' <summary>
		''' Runs the CreateArchive routine in the ViewModel.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub Archive_Click(sender As Object, e As RoutedEventArgs) Handles Archive.Click
			CType(Me.DataContext, ViewModels.ArchiveModel).CreateOrUpdateArchive()
		End Sub


		''' <summary>
		''' Allows selecting the location and name of the archive.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub Browse_Click(sender As Object, e As RoutedEventArgs) Handles Browse.Click
			With CType(Me.DataContext, ViewModels.ArchiveModel)
				Dim browser As New Microsoft.Win32.SaveFileDialog()
				browser.DefaultExt = ".q2qx"
				browser.Filter = "Cue2Q Projects (.q2qx)|*.q2qx"
				browser.AddExtension = True
				browser.CheckPathExists = True

				If .FilePath = "" Then
					browser.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
				Else
					browser.InitialDirectory = .FilePath
				End If

				browser.Title = "Create an Archive File"

				If browser.ShowDialog() = True Then
					.FileName = browser.SafeFileName
					.FilePath = New IO.FileInfo(browser.FileName).DirectoryName & "\"
				Else
					My.Application.Show.Console.SendMessage("The user canceled out of selecting an archive location.")
					Return
				End If


			End With
		End Sub

		Private Sub Closure_Click(sender As Object, e As RoutedEventArgs) Handles Closure.Click
			Me.Close()
		End Sub

		Private Sub Location_Click(sender As Object, e As RoutedEventArgs) Handles Location.Click
			Try
				With CType(Me.DataContext, ViewModels.ArchiveModel)
					If FileIO.FileSystem.FileExists(.FullPath) Then
						Process.Start("explorer.exe", "/select," & .FullPath)
					ElseIf FileIO.FileSystem.DirectoryExists(.FilePath) Then
						Process.Start("explorer.exe", "/open," & .FilePath)
					Else
						'Do nothing for now. Maybe raise some sort of warning?
					End If
				End With
			Catch
			End Try
		End Sub
	End Class
End Namespace